{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/yoyee/Documents/deepSfm\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "# add your module path\n",
    "module_path = os.path.abspath(os.path.join('..'))\n",
    "if module_path not in sys.path:\n",
    "    sys.path.append(module_path)\n",
    "# change your base path\n",
    "os.chdir('../')\n",
    "print(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "img:  tensor([[[[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,\n",
      "            14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,\n",
      "            28,  29],\n",
      "          [ 30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,\n",
      "            44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,\n",
      "            58,  59],\n",
      "          [ 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,\n",
      "            74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,\n",
      "            88,  89],\n",
      "          [ 90,  91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,\n",
      "           104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,\n",
      "           118, 119],\n",
      "          [120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,\n",
      "           134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,\n",
      "           148, 149],\n",
      "          [150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,\n",
      "           164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,\n",
      "           178, 179],\n",
      "          [180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,\n",
      "           194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,\n",
      "           208, 209],\n",
      "          [210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,\n",
      "           224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,\n",
      "           238, 239],\n",
      "          [240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,\n",
      "           254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267,\n",
      "           268, 269],\n",
      "          [270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283,\n",
      "           284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297,\n",
      "           298, 299],\n",
      "          [300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313,\n",
      "           314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327,\n",
      "           328, 329],\n",
      "          [330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343,\n",
      "           344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357,\n",
      "           358, 359],\n",
      "          [360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373,\n",
      "           374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387,\n",
      "           388, 389],\n",
      "          [390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403,\n",
      "           404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417,\n",
      "           418, 419],\n",
      "          [420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433,\n",
      "           434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447,\n",
      "           448, 449],\n",
      "          [450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463,\n",
      "           464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477,\n",
      "           478, 479],\n",
      "          [480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493,\n",
      "           494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507,\n",
      "           508, 509],\n",
      "          [510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523,\n",
      "           524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537,\n",
      "           538, 539],\n",
      "          [540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553,\n",
      "           554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567,\n",
      "           568, 569],\n",
      "          [570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583,\n",
      "           584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597,\n",
      "           598, 599]]]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "import torch.nn as nn\n",
    "\n",
    "to_floatTensor = lambda x: torch.tensor(x).type(torch.FloatTensor)\n",
    "\n",
    "from utils.utils import inv_warp_image_batch\n",
    "\n",
    "H, W = 20, 30\n",
    "img = torch.tensor(np.arange(H*W).reshape(H,W)).view(1, 1, H, W)\n",
    "print(\"img: \", img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "coor_cells:  torch.Size([20, 30, 2])\n",
      "tensor([[[-1.0000, -1.0000],\n",
      "         [-0.9310, -1.0000],\n",
      "         [-0.8621, -1.0000],\n",
      "         ...,\n",
      "         [ 0.8621, -1.0000],\n",
      "         [ 0.9310, -1.0000],\n",
      "         [ 1.0000, -1.0000]],\n",
      "\n",
      "        [[-1.0000, -0.8947],\n",
      "         [-0.9310, -0.8947],\n",
      "         [-0.8621, -0.8947],\n",
      "         ...,\n",
      "         [ 0.8621, -0.8947],\n",
      "         [ 0.9310, -0.8947],\n",
      "         [ 1.0000, -0.8947]],\n",
      "\n",
      "        [[-1.0000, -0.7895],\n",
      "         [-0.9310, -0.7895],\n",
      "         [-0.8621, -0.7895],\n",
      "         ...,\n",
      "         [ 0.8621, -0.7895],\n",
      "         [ 0.9310, -0.7895],\n",
      "         [ 1.0000, -0.7895]],\n",
      "\n",
      "        ...,\n",
      "\n",
      "        [[-1.0000,  0.7895],\n",
      "         [-0.9310,  0.7895],\n",
      "         [-0.8621,  0.7895],\n",
      "         ...,\n",
      "         [ 0.8621,  0.7895],\n",
      "         [ 0.9310,  0.7895],\n",
      "         [ 1.0000,  0.7895]],\n",
      "\n",
      "        [[-1.0000,  0.8947],\n",
      "         [-0.9310,  0.8947],\n",
      "         [-0.8621,  0.8947],\n",
      "         ...,\n",
      "         [ 0.8621,  0.8947],\n",
      "         [ 0.9310,  0.8947],\n",
      "         [ 1.0000,  0.8947]],\n",
      "\n",
      "        [[-1.0000,  1.0000],\n",
      "         [-0.9310,  1.0000],\n",
      "         [-0.8621,  1.0000],\n",
      "         ...,\n",
      "         [ 0.8621,  1.0000],\n",
      "         [ 0.9310,  1.0000],\n",
      "         [ 1.0000,  1.0000]]])\n"
     ]
    }
   ],
   "source": [
    "device = 'cpu'\n",
    "Batch, channel, H, W = img.shape\n",
    "coor_cells = torch.stack(torch.meshgrid(torch.linspace(-1, 1, W), torch.linspace(-1, 1, H)), dim=2)\n",
    "coor_cells = coor_cells.transpose(0, 1)\n",
    "coor_cells = coor_cells.to(device)\n",
    "coor_cells = coor_cells.contiguous()\n",
    "print(\"coor_cells: \", coor_cells.shape)\n",
    "print(coor_cells)\n",
    "homography = torch.tensor(np.identity(3)).unsqueeze(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yoyee/.local/lib/python3.6/site-packages/ipykernel_launcher.py:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  \"\"\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[[[  0.0000,   1.0000,   2.0000,   3.0000,   4.0000,   5.0000,   6.0000,\n",
       "             7.0000,   8.0000,   9.0000,  10.0000,  11.0000,  12.0000,  13.0000,\n",
       "            14.0000,  15.0000,  16.0000,  17.0000,  18.0000,  19.0000,  20.0000,\n",
       "            21.0000,  22.0000,  23.0000,  24.0000,  25.0000,  26.0000,  27.0000,\n",
       "            28.0000,  29.0000],\n",
       "          [ 30.0000,  31.0000,  32.0000,  33.0000,  34.0000,  35.0000,  36.0000,\n",
       "            37.0000,  38.0000,  39.0000,  40.0000,  41.0000,  42.0000,  43.0000,\n",
       "            44.0000,  45.0000,  46.0000,  47.0000,  48.0000,  49.0000,  50.0000,\n",
       "            51.0000,  52.0000,  53.0000,  54.0000,  55.0000,  56.0000,  57.0000,\n",
       "            58.0000,  59.0000],\n",
       "          [ 60.0000,  61.0000,  62.0000,  63.0000,  64.0000,  65.0000,  66.0000,\n",
       "            67.0000,  68.0000,  69.0000,  70.0000,  71.0000,  72.0000,  73.0000,\n",
       "            74.0000,  75.0000,  76.0000,  77.0000,  78.0000,  79.0000,  80.0000,\n",
       "            81.0000,  82.0000,  83.0000,  84.0000,  85.0000,  86.0000,  87.0000,\n",
       "            88.0000,  89.0000],\n",
       "          [ 90.0000,  91.0000,  92.0000,  93.0000,  94.0000,  95.0000,  96.0000,\n",
       "            97.0000,  98.0000,  99.0000, 100.0000, 101.0000, 102.0000, 103.0000,\n",
       "           104.0000, 105.0000, 106.0000, 107.0000, 108.0000, 109.0000, 110.0000,\n",
       "           111.0000, 112.0000, 113.0000, 114.0000, 115.0000, 116.0000, 117.0000,\n",
       "           118.0000, 119.0000],\n",
       "          [120.0000, 121.0000, 122.0000, 123.0000, 124.0000, 125.0000, 126.0000,\n",
       "           127.0000, 128.0000, 129.0000, 130.0000, 131.0000, 132.0000, 133.0000,\n",
       "           134.0000, 135.0000, 136.0000, 137.0000, 138.0000, 139.0000, 140.0000,\n",
       "           141.0000, 142.0000, 143.0000, 144.0000, 145.0000, 146.0000, 147.0000,\n",
       "           148.0000, 149.0000],\n",
       "          [150.0000, 151.0000, 152.0000, 153.0000, 154.0000, 155.0000, 156.0000,\n",
       "           157.0000, 158.0000, 159.0000, 160.0000, 161.0000, 162.0000, 163.0000,\n",
       "           164.0000, 165.0000, 166.0000, 167.0000, 168.0000, 169.0000, 170.0000,\n",
       "           171.0000, 172.0000, 173.0000, 174.0000, 175.0000, 176.0000, 177.0000,\n",
       "           178.0000, 179.0000],\n",
       "          [180.0000, 181.0000, 182.0000, 183.0000, 184.0000, 185.0000, 186.0000,\n",
       "           187.0000, 188.0000, 189.0000, 190.0000, 191.0000, 192.0000, 193.0000,\n",
       "           194.0000, 195.0000, 196.0000, 197.0000, 198.0000, 199.0000, 200.0000,\n",
       "           201.0000, 202.0000, 203.0000, 204.0000, 205.0000, 206.0000, 207.0000,\n",
       "           208.0000, 209.0000],\n",
       "          [210.0000, 211.0000, 212.0000, 213.0000, 214.0000, 215.0000, 216.0000,\n",
       "           217.0000, 218.0000, 219.0000, 220.0000, 221.0000, 222.0000, 223.0000,\n",
       "           224.0000, 225.0000, 226.0000, 227.0000, 228.0000, 229.0000, 230.0000,\n",
       "           231.0000, 232.0000, 233.0000, 234.0000, 235.0000, 236.0000, 237.0000,\n",
       "           238.0000, 239.0000],\n",
       "          [240.0000, 241.0000, 242.0000, 243.0000, 244.0000, 245.0000, 246.0000,\n",
       "           247.0000, 248.0000, 249.0000, 250.0000, 251.0000, 252.0000, 253.0000,\n",
       "           254.0000, 255.0000, 256.0000, 257.0000, 258.0000, 259.0000, 260.0000,\n",
       "           261.0000, 262.0000, 263.0000, 264.0000, 265.0000, 266.0000, 267.0000,\n",
       "           268.0000, 269.0000],\n",
       "          [270.0000, 271.0000, 272.0000, 273.0000, 274.0000, 275.0000, 276.0000,\n",
       "           277.0000, 278.0000, 279.0000, 280.0000, 281.0000, 282.0000, 283.0000,\n",
       "           284.0000, 285.0000, 286.0000, 287.0000, 288.0000, 289.0000, 290.0000,\n",
       "           291.0000, 292.0000, 293.0000, 294.0000, 295.0000, 296.0000, 297.0000,\n",
       "           298.0000, 299.0000],\n",
       "          [300.0000, 301.0000, 302.0000, 303.0000, 304.0000, 305.0000, 306.0000,\n",
       "           307.0000, 308.0000, 309.0000, 310.0000, 311.0000, 312.0000, 313.0000,\n",
       "           314.0000, 315.0000, 316.0000, 317.0000, 318.0000, 319.0000, 320.0000,\n",
       "           321.0000, 322.0000, 323.0000, 324.0000, 325.0000, 326.0000, 327.0000,\n",
       "           328.0000, 329.0000],\n",
       "          [330.0000, 331.0000, 332.0000, 333.0000, 334.0000, 335.0000, 336.0000,\n",
       "           337.0000, 338.0000, 339.0000, 340.0000, 341.0000, 342.0000, 343.0000,\n",
       "           344.0000, 345.0000, 346.0000, 347.0000, 348.0000, 349.0000, 350.0000,\n",
       "           351.0000, 352.0000, 353.0000, 354.0000, 355.0000, 356.0000, 357.0000,\n",
       "           358.0000, 359.0000],\n",
       "          [360.0000, 361.0000, 362.0000, 363.0000, 364.0000, 365.0000, 366.0000,\n",
       "           367.0000, 368.0000, 369.0000, 370.0000, 371.0000, 372.0000, 373.0000,\n",
       "           374.0000, 375.0000, 376.0000, 377.0000, 378.0000, 379.0000, 380.0000,\n",
       "           381.0000, 382.0000, 383.0000, 384.0000, 385.0000, 386.0000, 387.0000,\n",
       "           388.0000, 389.0000],\n",
       "          [390.0000, 391.0000, 392.0000, 393.0000, 394.0000, 395.0000, 396.0000,\n",
       "           397.0000, 398.0000, 399.0000, 400.0000, 401.0000, 402.0000, 403.0000,\n",
       "           404.0000, 405.0000, 406.0000, 407.0000, 408.0000, 409.0000, 410.0000,\n",
       "           411.0000, 412.0000, 413.0000, 414.0000, 415.0000, 416.0000, 417.0000,\n",
       "           418.0000, 419.0000],\n",
       "          [420.0000, 421.0000, 422.0000, 423.0000, 424.0000, 425.0000, 426.0000,\n",
       "           427.0000, 428.0000, 429.0000, 430.0000, 431.0000, 432.0000, 433.0000,\n",
       "           434.0000, 435.0000, 436.0000, 437.0000, 438.0000, 439.0000, 440.0000,\n",
       "           441.0000, 442.0000, 443.0000, 444.0000, 445.0000, 446.0000, 447.0000,\n",
       "           448.0000, 449.0000],\n",
       "          [450.0000, 451.0000, 452.0000, 453.0000, 454.0000, 455.0000, 456.0000,\n",
       "           457.0000, 458.0000, 459.0000, 460.0000, 461.0000, 462.0000, 463.0000,\n",
       "           464.0000, 465.0000, 466.0000, 467.0000, 468.0000, 469.0000, 470.0000,\n",
       "           471.0000, 472.0000, 473.0000, 474.0000, 475.0000, 476.0000, 477.0000,\n",
       "           478.0000, 479.0000],\n",
       "          [480.0000, 481.0000, 482.0000, 483.0000, 484.0000, 485.0000, 486.0000,\n",
       "           487.0000, 488.0000, 489.0000, 490.0000, 491.0000, 492.0000, 493.0000,\n",
       "           494.0000, 495.0000, 496.0000, 497.0000, 498.0000, 499.0000, 500.0000,\n",
       "           501.0000, 502.0000, 503.0000, 504.0000, 505.0000, 506.0000, 507.0000,\n",
       "           508.0000, 509.0000],\n",
       "          [510.0000, 511.0000, 512.0000, 513.0000, 514.0000, 515.0000, 516.0000,\n",
       "           517.0000, 518.0000, 519.0000, 520.0000, 521.0000, 522.0000, 523.0000,\n",
       "           524.0000, 525.0000, 526.0000, 527.0000, 528.0000, 529.0000, 530.0000,\n",
       "           531.0000, 532.0000, 533.0000, 534.0000, 535.0000, 536.0000, 537.0000,\n",
       "           538.0000, 539.0000],\n",
       "          [540.0000, 541.0000, 542.0000, 543.0000, 544.0000, 545.0000, 546.0000,\n",
       "           547.0000, 548.0000, 549.0000, 550.0000, 551.0000, 552.0000, 553.0000,\n",
       "           554.0000, 555.0000, 556.0000, 557.0000, 558.0000, 559.0000, 560.0000,\n",
       "           561.0000, 562.0000, 563.0000, 564.0000, 565.0000, 566.0000, 567.0000,\n",
       "           568.0000, 569.0000],\n",
       "          [570.0000, 571.0000, 572.0000, 573.0000, 574.0000, 575.0000, 576.0000,\n",
       "           577.0000, 578.0000, 579.0000, 580.0000, 581.0000, 582.0000, 583.0000,\n",
       "           584.0000, 585.0000, 586.0000, 587.0000, 588.0000, 589.0000, 590.0000,\n",
       "           591.0000, 592.0000, 593.0000, 594.0000, 595.0000, 596.0000, 597.0000,\n",
       "           598.0000, 599.0000]]]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inv_warp_image_batch(to_floatTensor(img), to_floatTensor(homography), mode = 'bilinear')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py36_pytorch",
   "language": "python",
   "name": "py36_pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
